package com.cy1640.damai.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cy1640.damai.R;
import com.cy1640.damai.bean.Yanchu;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;

/**
 * Created by chenyang on 2016/12/12.
 */

public class MyItemBaseAdapter extends BaseAdapter {
    private static final String BASR_URL1 = "http://pimg.damai.cn/perform/project/";
    private Context context;
    private Map<String, Bitmap> map;
    private List<Yanchu> lists;
    public MyItemBaseAdapter(Context context, List<Yanchu> lists) {
        this.context = context;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int i) {
        return lists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    ViewHolder holder=null;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            // 按当前所需的样式，确定new的布局
            convertView = LayoutInflater.from(context).inflate(R.layout.item_class, null);
            holder = new ViewHolder();
            holder.classTitle = (TextView) convertView.findViewById(R.id.class_title);
            holder.classTime = (TextView) convertView.findViewById(R.id.class_time);
            holder.classPrice = (TextView) convertView.findViewById(R.id.class_price);
            holder.classTheatre = (TextView) convertView.findViewById(R.id.class_theatre);

            holder.classSeat = (TextView) convertView.findViewById(R.id.class_seat);
            holder.classState = (TextView) convertView.findViewById(R.id.class_state);
            holder.classSummery = (TextView) convertView.findViewById(R.id.class_summery);
            holder.classImg = (ImageView) convertView.findViewById(R.id.class_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.classTitle.setText(lists.get(position).getN());
        holder.classTime.setText(lists.get(position).getT());
        holder.classPrice.setText(lists.get(position).getPriceName());
        holder.classTheatre.setText(lists.get(position).getVenName());
        String s=lists.get(position).getSummary();
        if (TextUtils.isEmpty(s)) {
            holder.classSummery.setVisibility(View.GONE);
            holder.classSeat.setVisibility(View.VISIBLE);
            holder.classState.setVisibility(View.VISIBLE);

            holder.classSeat.setText("座");
            holder.classState.setText("售票中");
        } else {
            holder.classSummery.setVisibility(View.VISIBLE);
            holder.classState.setVisibility(View.GONE);
            holder.classSummery.setText("『" + lists.get(position).getSummary() + "』");
            holder.classSeat.setVisibility(View.VISIBLE);
            holder.classSeat.setText("预售中");
        }

//        holder.classImg.setImageResource(R.mipmap.ic_launcher);

        String i = String.valueOf(lists.get(position).getI());

        String imageUrl = BASR_URL1 + i.substring(0, i.length()-2) + "/" + i+"_n.jpg";
        Picasso.with(context).load(imageUrl).into(holder.classImg);

        return convertView;
    }

    static class ViewHolder{
        TextView classTitle,classSeat,classState,classTime,classTheatre,classPrice,classSummery;
        ImageView classImg;
    }
}
